/***
 * * PROJECT
 * *    Logical Neighborhoods - J2ME Run-Time Support 
 * * VERSION
 * *    $LastChangedRevision: 22 $
 * * DATE
 * *    $LastChangedDate: 2007-05-06 11:55:23 +0200 (Sun, 06 May 2007) $
 * * LAST_CHANGE_BY
 * *    $LastChangedBy: lmottola $
 * *
 * *    $Id: ProfAdv.java 22 2007-05-06 11:55:23 +0200 (Sun, 06 May 2007) lmottola $
 * *
 * *   Logical Neighborhoods - 
 * *                   Programming Wireless Sensor Networks
 * *                   (One Slice at a Time)
 * *
 * *   This program is free software; you can redistribute it and/or
 * *   modify it under the terms of the GNU Lesser General Public License
 * *   as published by the Free Software Foundation; either version 2.1
 * *   of the License, or (at your option) any later version.
 * *
 * *   This program is distributed in the hope that it will be useful,
 * *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 * *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * *   GNU General Public License for more details.
 * *
 * *   You should have received a copy of the GNU Lesser General Public 
 * *   License along with this program; if not, you may find a copy at 
 * *   the FSF web site at 'www.gnu.org' or 'www.fsf.org', or you may 
 * *   write to the Free Software Foundation, Inc., 59 Temple 
 * *   Place - Suite 330, Boston, MA  02111-1307, USA
 ***/

package atag.runtime.ln.messages;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Vector;
import atag.runtime.ln.nodeAttributes.*;

/**
 * @author Luca Mottola <a
 *         href="mailto:mottola@elet.polimi.it">mottola@elet.polimi.it</a>
 * 
 */
public class ProfAdv extends LNMessage {

	private Vector nodeAttributes;

	private int cost;
    private int interfaceId;

	public ProfAdv(int id, long seqNumber, Vector nodeAttrs) {

		super(id, seqNumber);
		this.cost = 0;
		this.nodeAttributes = new Vector();
		for (int i = 0; i < nodeAttrs.size(); i++) {
			nodeAttributes.addElement(nodeAttrs.elementAt(i));
		}
	}

	public int getCost() {
		return cost;
	}

	public void setCost(int cost) {
		this.cost = cost;
	}

	public Vector getNodeAttributes() {
		return nodeAttributes;
	}

    public void setInterfaceId(int id)
    {
        this.interfaceId = id;
    }

    public int getInterfaceId()
    {
        return this.interfaceId;
    }

    public DataOutput serialize(DataOutput dg) throws IOException {

		dg.writeByte(LNMessage.PROFADV_MSG_TYPE);
		dg = super.serialize(dg);

        dg.writeInt(cost);

        dg.writeInt(this.nodeAttributes.size());
        for(int i= 0; i < this.nodeAttributes.size(); i++)
        {
            NodeAttribute na = (NodeAttribute)this.nodeAttributes.elementAt(i);
            na.serialize(dg);
        }

		return dg;
	}

	public static ProfAdv deserialize(DataInput dg, int senderId,
			int originatorId, long seqNum, byte hops) throws IOException {


        int cost = dg.readInt();

        int size = dg.readInt();
        Vector nodeAttributes = new Vector();
        for (int i = 0; i < size; i++)
        {
          NodeAttribute na = NodeAttribute.deserialize(dg);
          nodeAttributes.addElement(na);
        }
        ProfAdv profAdv = new ProfAdv(originatorId,seqNum,nodeAttributes);
        profAdv.setCost(cost);
        profAdv.setSenderId(senderId);
		return profAdv;
	}
}
